# -*- coding: utf-8 -*-
'''
@author: Hugo
@file: jindong.py.py
@time: 2021/8/15 13:36
'''
import json
import pymongo
from mitmproxy import ctx
client = pymongo.MongoClient()
db = client['python']
coll = db['jingdong']

def response(flow):
    # response = flow.response()
    # ctx.log.warn(str(response.text))
    # 京东搜索入口 API 地址:  urls = 'https://106.39.169.231/client.action?functionId=search&clientVersion=10.0.8&build=89053&client=android&d_brand=OnePlus&d_model=GM1900&osVersion=5.1.1&screen=192'
    url = flow.request.url
    if 'client.action?functionId=search' not in url:
        return
    data = json.loads(flow.response.text)
    info = data["wareInfo"]
    for i in info:
        new = {}
        new['标题']=i.get('wname')
        new["价格"]=i.get('jdPrice')
        new["评论"]=i.get('reviews')
        coll.insert_one(new)

